
;(function (RCABase) {
    var jc_init={
        extend: {
            ajax_url: '/admin/Aftersale/ajax',
            weibaofinish_url: '/admin/Service/weibaofinish',
            sex_1:'男',
            sex_2:'女',
        },
        sex:function(i) {
            arr[0] = "";
            arr[1] = "男";
            arr[2] = "女";
            return arr[i];
        },
        cartType:function(i){
            arr[0] = "";
            arr[1] = "身份证";
            arr[2] = "护照";
            arr[3] = "军人证";
            arr[4] = "港澳身份证";
            return arr[i];
        },
        SendRecords:function () {
            $('#SendRecords').bootstrapTable({
                url: jc_init.extend.ajax_url+"?t=10",
                pageNumber: 1, //初始化加载第一页
                pagination: true,//是否分页
                sidePagination: 'server',//server:服务器端分页|client：前端分页
                pageSize: 10,//单页记录数
                pageList: [5, 10, 20],//可选择单页记录数
                showRefresh: false,//刷新按钮
                showColumns: false, //
                columns: [
                    {checkbox:true},
                    {field: 'add_time', title: '操作时间'},
                    {field: 'dealer_user', title: '办理人信息',  events: "operateEvents",formatter: function (value, row, index) {
                            return ' <a href="javascript:"  class="buyer">'+value+'</a>'}},
                    {field: 'dead_name', title: '逝者信息',  events: "operateEvents",formatter: function (value, row, index) {
                            return ' <a href="javascript:"  class="deceased">'+value+'</a>'}},
                    {field: 'deposit_time', title: '寄存时间'},
                    {field: 'convergence_type', title: '收敛形式'},
                    {field: 'cremation_certificate_img', title: '火化证明', events: "operateEvents",formatter: function (value, row, index) {
                            return '<a href="javascript:;" class="lookImg">查看</a> ' +
                            '| <a href="javascript:;" class="export">导出</a>'}},
                    {field: 'url_file_original', title: '原证回收', events: "operateEvents",formatter: function (value, row, index) {
                            return '<a href="javascript:;" class="lookImgs"  >查看</a> ' +
                            '| <a href="javascript:;" class="export">导出</a>'}},
                    {field: 'deposit_time_out', title: '寄出时间'},
                    {field: 'handle_name', title: '寄出类型'},
                    {field: ' ', title: '接待人'},
                    {field: 'submit_status_name', title: '状态反馈'},
                    {field: 'confirm_content', title: '备注'},
                ],
                queryParams: function (params) {
                    //上传服务器的参数
                    var temp = {
                        page: params.offset + 0,// SQL语句起始索引
                        limit: params.limit  // 每页显示数量
                    };
                    return temp;
                },
                formatLoadingMessage: function () {
                    return "数据正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
            });


        },
        RenewalRecordPay:function() {
            $('#RenewalRecordPay').bootstrapTable({
                url: jc_init.extend.ajax_url+"?t=11",
                pageNumber: 1, //初始化加载第一页
                pagination: true,//是否分页
                sidePagination: 'server',//server:服务器端分页|client：前端分页
                pageSize: 10,//单页记录数
                pageList: [5, 10, 20],//可选择单页记录数
                showRefresh: false,//刷新按钮
                showColumns: false, //
                columns: [
                    {checkbox:true},
                    {field: 'add_time', title: '操作时间'},
                    {field: 'buyer_name', title: '办理人信息',  events: "operateEvents",formatter: function (value, row, index) {
                            return ' <a href="javascript:"  class="buyer">'+value+'</a>'}},
                    {field: 'deceased_name', title: '逝者信息',  events: "operateEvents",formatter: function (value, row, index) {
                            return ' <a href="javascript:"  class="deceased">'+value+'</a>'}},
                    {field: 'deposit_time', title: '寄存时间'},
                    {field: 'convergence_type', title: '收敛形式'},
                    {field: 'cremation_certificate_img', title: '火化证明',events: "operateEvents",formatter: function (value, row, index) {
                            return '<a href="javascript:;" class="lookImg">查看</a> ' +
                                '| <a href="javascript:;" class="export">导出</a>'}},
                    {field: 'deposit_cost', title: '续费标准'},
                    {field: 'storage_time', title: '续费时长'},
                    {field: 'original_price', title: '政策减免'},
                    {field: 'total_price', title: '应收金额'},
                    {field: 'actual_amount', title: '实收金额'},
                    {field: 'pay_name   ', title: '接待人'},
                    {field: 'extension_time', title: '延期至'},
                    {field: 'remark', title: '备注'},
                ],
                queryParams: function (params) {
                    //上传服务器的参数
                    var temp = {
                        page: params.offset + 0,// SQL语句起始索引
                        limit: params.limit  // 每页显示数量
                    };
                    return temp;
                },
                formatLoadingMessage: function () {
                    return "数据正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
            });
        },
        openDeceased:function (n) {
            var html= '<form class="form-horizontal result_form " action="">\n' +
                '            <fieldset>\n' +
                '                <div  class="control-group1-p result_dep" style="border:1px solid #e2e2e2; padding:20px 0 0 20px;">\n' +
                '                    <div class="row"  style="margin-left: 0;margin-right: 0;">\n' +
                '                        <div class="col-md-6">\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">逝者姓名</label>\n' +
                '                                <div class="col-lg-9">' +
                '                                    <input type="text" class="form-control" disabled value="'+n.deceased_name+'" disabled >\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label" >出生日期</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="buyer_phone"   value="'+n.date_birth+'" disabled>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                        <div class="col-md-6">\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">性别</label>\n' +
                '                                <div class="col-lg-9">\n' +
            '                                    <input type="text" class="form-control total_price" name="total_price"  value="'+n.gender+'" disabled>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">逝世日期</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control total_price" name="total_price"  value="'+n.date_death+'" disabled>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">与逝者关系</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control"  name="second_phone"  value="'+n.relationship_users+'" disabled>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                    </div>\n' +
                '                </div>\n' +
                '            </fieldset>\n' +
                '        </form>';

            RCABase.initBase.ajaxLayerOpen('',html,['750px','230px'])
            return html;
        },
        openbuyer:function (n) {
            console.log(n)
            var html= '<form class="form-horizontal result_form " action="">\n' +
                '            <fieldset>\n' +
                '                <div  class="control-group1-p result_dep" style="border:1px solid #e2e2e2; padding:20px 0 0 20px;">\n' +
                '                    <div class="row"  style="margin-left: 0;margin-right: 0;">\n' +
                '                        <div class="col-md-6">\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">办理人姓名</label>\n' +
                '                                <div class="col-lg-9">' +
                '                                    <input type="text" class="form-control" name="buyer_name" value="'+n.buyer_name+'" disabled >\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label" >手机号码</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="buyer_phone"  disabled value="'+n.buyer_phone+'" >\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">联系地址</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="cart_address" disabled value="'+n.cart_address+'"  >\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">与逝者关系</label>\n' +
                '                                <div class="col-sm-9">\n' +
                '                                    <input type="text" class="form-control" name="relationship" disabled value="'+n.relationship+'"  placeholder="请输入与逝者关系">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">经办人</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="second_name js-date" disabled value="'+n.handle_name+'" />\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                        <div class="col-md-6">\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">性别</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="cart_address" disabled value="'+n.buyer_sex+'" disabled  >\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">证件类型</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control total_price" name="total_price" disabled value="'+n.cart_type+'" placeholder="">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">身份证号</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="cart_num" disabled value="'+n.cart_num+'">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">证件查看</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                   <a href="javascript:;" class="lookImgss" data-url="'+n.file_url+'"  >查看</a>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">经办时间</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control"  name="second_phone"  value="'+n.handle_time+'" disabled>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                    </div>\n' +
                '                </div>\n' +
                '            </fieldset>\n' +
                '        </form>';
            RCABase.initBase.ajaxLayerOpen('',html,['750px','380px'])
            return html;
        },
        ajaxLayerOpen: function (title, html, area) {
            layer.open({
                type: 1,
                icon: 4,
                title: title,
                anim: 2,
                area: area,
                content: html
            });
        },
    };
    window.operateEvents = {
        'click .deceased': function (e, value, row, index) {
            console.log(row.deceased)
            jc_init.openDeceased(row.deceased)
        },
        'click .buyer': function (e, value, row, index) {
            console.log(row.deceased)
            jc_init.openbuyer(row.handle)
        },
        'click .lookImg': function (e, value, row, index) {
            RCABase.initBase.lookLayerImg(row.cremation_certificate_img)
        },
        'click .lookImgs': function (e, value, row, index) {
            RCABase.initBase.lookLayerImg(row.cremation_certificate_img)
        },
        'click .export': function (e, value, row, index) {
            RCABase.initBase.lookLayerImg(row.cremation_certificate_img)
        },

    };


    jc_init.SendRecords();
    jc_init.RenewalRecordPay();
    //切换显示
    $(document).on('click','.nav_tabs li',function(){
        $(this).addClass('active').siblings().removeClass('active');
        var index = $(".nav_tabs li").index(this);
        $('.content_nav').eq(index).removeClass("feature").siblings().addClass("feature");
    });
    // 提交订单
    $("#check-btn").click(function(){
        $.post($("form").attr('action'), $("form").serialize(), function(result){
            if(result.code == 1){
                layer.msg(result.msg);
            } else {
                layer.msg(result.msg);
            }
        }, 'json');
    });
    // 寄出 提交
    $("#check-btn-submit").click(function(){
        console.log($(".form_save_jc").attr('action'))
        $("#submit_status").val(2)
        $.post($(".form_save_jc").attr('action'), $(".form_save_jc").serialize(), function(result){
            if(result.code == 1){
                layer.alert('提交成功', {
                    icon: 1, yes: function (index) {
                        layer.close(index);
                        window.location.href = result.url;
                    }
                })
            } else {
                layer.msg(result.msg);
            }
        }, 'json');
    });
    // 寄出 提价
    $("#check-save").click(function(){
        console.log($(".form_save_jc").attr('action'))
        $("#submit_status").val(1)
        $.post($(".form_save_jc").attr('action'), $(".form_save_jc").serialize(), function(result){
            if(result.code == 1){
                layer.msg(result.msg);
            } else {
                layer.msg(result.msg);
            }
        }, 'json');
    });
    // 寄出 提价
    $("#check-btn-print").click(function(){
        console.log($(".form_save_jc").attr('action'))

        $.post($(".form_save_jc").attr('action'), $(".form_save_jc").serialize(), function(result){
            if(result.code == 1){


                layer.msg(result.msg);
            } else {
                layer.msg(result.msg);
            }
        }, 'json');
    });
    $('#Renewaldue').click(function () {
        var original_price_op='',storage_time_op='',
            deposit_cost=$(this).data('deposit_cost'),
            deposit_id=$(this).data('deposit_id'),
            url = $(this).data('url');
        for (var i=0;i<10;i++){
            original_price_op+=' <option value="'+i/10+'">'+i+'%</option>';
        }
        for (var i=1;i<15;i++){
            storage_time_op+=' <option value="'+i+'">'+i+'</option>'
        }
         layer.open({
            type: 1, icon:4, //图标
            title:['寄存续费','color:#383838;font-size:16px;'],
            anim: 2, btn: [  '提交'], area: ['750px','480px'], //宽
             content:
                '<form class="form-horizontal result_form " action="">\n' +
                '            <fieldset>\n' +
                '                <div  class="control-group1-p result_dep" style="border:1px solid #e2e2e2; padding:20px 0 0 20px;">\n' +
                '                    <div class="row"  style="margin-left: 0;margin-right: 0;">\n' +
                '                        <div class="col-md-6">\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">办理人姓名</label>\n' +
                '                                <div class="col-lg-9">' +
                 '                                  <input type="hidden" name="type"  value="1" >\n' +
                 '                                  <input type="hidden" name="deposit_id"  value="'+deposit_id+'" >\n' +
                '                                    <input type="text" class="form-control" name="buyer_name" placeholder="请输入办理人姓名" value="" >\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label" >手机号码</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="buyer_phone"   value=""  placeholder="请输入手机号码">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">联系地址</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="cart_address"  value=""  placeholder="请输入联系地址">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">与逝者关系</label>\n' +
                '                                <div class="col-sm-9">\n' +
                '                                    <input type="text" class="form-control" name="relationship"  value=""  placeholder="请输入与逝者关系">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">续费标椎</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <select name="deposit_cost" class="form-control deposit_cost">\n' +
                '                                            <option value="'+deposit_cost+'">'+deposit_cost+'</option>\n' +
                '                                    </select>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">政策减免</label>\n' +
                '                                <div class="col-lg-9">\n' +
                 '                                    <input type="text" class="form-control original_price" name="original_price"  id="original_price"  value=""  placeholder="请输入金额">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">免费几年</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <select  id="mianfei" class="form-control mianfei">\n' +
                 '                                            <option value="0">无免费</option>\n' +
                 '                                            <option value="1">免费寄存1年</option>\n' +
                 '                                            <option value="2">免费寄存2年</option>\n' +
                 '                                            <option value="3">免费寄存3年</option>\n' +
                '                                    </select>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                        <div class="col-md-6">\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">性别</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <select name="buyer_sex" class="form-control">\n' +
                '                                        <option value="1">男</option>\n' +
                '                                        <option value="2">女</option>\n' +
                '                                    </select>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">证件类型</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <select name="cart_type" class="form-control">\n' +
                '                                        <option value="1">身份证</option>\n' +
                '                                        <option value="2">护照</option>\n' +
                '                                        <option value="3">军人证</option>\n' +
                '                                        <option value="4">港澳身份证</option>\n' +
                '                                    </select>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">身份证号</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control" name="cart_num"    placeholder="请输入身份证号">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">交费方式</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <select name="pay_type"  class="form-control">\n' +
                '                                        <option value="1">现金</option>\n' +
                '                                        <option value="2">转账</option>\n' +
                '                                        <option value="3">电汇</option>\n' +
                '                                        <option value="4">微信</option>\n' +
                '                                        <option value="5">支付宝</option>\n' +
                '                                        <option value="6">其他</option>\n' +
                '                                    </select>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">续费时长</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <select class="form-control storage_time" id="storage_time" name="storage_time">\n' +storage_time_op+
                '                                    </select>\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">应收金额</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control total_price" name="total_price"  value="" placeholder="">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                            <div class="form-group">\n' +
                '                                <label class="control-label">实收金额</label>\n' +
                '                                <div class="col-lg-9">\n' +
                '                                    <input type="text" class="form-control actual_amount"  name="actual_amount"  value=" " placeholder="">\n' +
                '                                </div>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                    </div>\n' +
                '                </div>\n' +
                '            </fieldset>\n' +
                '        </form>',
             yes:function(index, layero){
                 var result_form = $(".result_form").serialize();
                 console.log(result_form)
                 $.post(url, $(".result_form").serialize(), function(result) {
                     if(result.code == 1){
                         layer.alert('提交成功', {
                             icon: 1, yes: function (index) {
                                 layer.close(index);
                                 window.location.href = result.url;
                             }
                         })
                     } else {
                         layer.msg(result.msg);
                     }
                 })
             }
        });
    });
    $(document).on('change','.deposit_cost',function () {
        jc_init.jisuan()
    });
    $(document).on('change','.mianfei',function () {
        //免费几年
        var  result_form = $('.result_form');
        var deposit_cost = result_form.find('.deposit_cost').children('option:selected').val();
        var mianfei = $(this).val()*deposit_cost;
        var storage_time = result_form.find('.storage_time').val();
        result_form.find('#original_price').val(mianfei);

        var total_price = (deposit_cost*storage_time);
        var actual_amount =total_price-mianfei;
        result_form.find('.total_price').val(total_price.toFixed(2))
        result_form.find('.actual_amount').val(actual_amount.toFixed(2))

    });
    $(document).on('click','.lookImgss',function () {
        initBase.initBase.lookLayerImg($(this).data('url'))
    });

    $(document).on('change','.storage_time',function (event) {
        var  result_form = $('.result_form');
        var deposit_cost = result_form.find('.deposit_cost').children('option:selected').val();
        var original_price = $('#original_price').val();
        var storage_time = ( $(this).val());

        var num = (deposit_cost*storage_time);
        if (original_price == 0){
            result_form.find('.total_price').val(num.toFixed(2))
            result_form.find('.actual_amount').val(num.toFixed(2))
        }else {
            var total_price =num-original_price;
            result_form.find('.total_price').val(num.toFixed(2))
            result_form.find('.actual_amount').val(total_price.toFixed(2))
        }
    });
    $(document).on('blur','.original_price',function () {
        var  result_form = $('.result_form');
        var deposit_cost = result_form.find('.deposit_cost').children('option:selected').val();
        var storage_time = $('#storage_time').children('option:selected').val();
        var original_price = ( $(this).val());

        var num = (deposit_cost*storage_time);
        if (original_price == 0){
            result_form.find('.total_price').val(num.toFixed(2))
            result_form.find('.actual_amount').val(num.toFixed(2))
        }else {
            var total_price =num-original_price;
            result_form.find('.total_price').val(num.toFixed(2))
            result_form.find('.actual_amount').val(total_price.toFixed(2))
        }
    });
    RCABase.init_jc = jc_init;
})(RCABase);